<template>
  <div class="order-page">
    <div class="order-type">
      <div
        class="type"
        @click="jumpTo('/user/order/0')"
        :class="{ active: query.status === 0 }"
      >
        <span>待确定</span>
      </div>
      <div
        class="type"
        @click="jumpTo('/user/order/1')"
        :class="{ active: query.status === 1 }"
      >
        <span>待发货</span>
      </div>
      <div
        class="type"
        @click="jumpTo('/user/order/2')"
        :class="{ active: query.status === 2 }"
      >
        <span>待收货</span>
      </div>
      <div
        class="type"
        @click="jumpTo('/user/order/3')"
        :class="{ active: query.status === 3 }"
      >
        <span>已完成</span>
      </div>
    </div>
    <div class="order-query">
      <div class="select">
        <el-checkbox v-model="select">全选</el-checkbox>
        <span>批量确认收货</span>
      </div>
      <div class="flex"></div>
      <div class="search">
        <el-input
          size="small"
          placeholder="商品名称/订单ID，并按回车键确定"
          prefix-icon="el-icon-search"
          v-model="query.search"
          @keypress.native.enter="getOrderList"
        >
        </el-input>
      </div>
    </div>
    <div class="order-list">
      <OrderItem
        :order="item"
        v-for="(item, index) in orderList"
        @change="getOrderList"
        :key="index"
      />
    </div>
    <div class="no-list" v-if="!orderList.length">
      <img src="../../../assets/order.657236e9.png" alt="" />
      暂无相关订单~
    </div>
    <div class="pages-box">
      <Pages2
        v-model="query.current"
        :jump="true"
        :size="query.length"
        :count="query.total"
        @change="getOrderList"
      />
    </div>
  </div>
</template>

<script>
import api from "@/api";
import { mapState } from "vuex";
import OrderItem from "@/components/OrderItem/OrderItem.vue";
import Pages2 from "@/components/Pages/Pages2.vue";

export default {
  name: "UserCoupon",
  components: { OrderItem, Pages2 },
  computed: {
    ...mapState("user", ["userinfo"]),
  },
  data() {
    return {
      select: false,
      query: {
        current: 1,
        length: 8,
        status: null,
        search: "",
        total: 0,
      },
      orderList: [],
    };
  },
  watch: {
    "$route.params.type": function () {
      let status = this.$route.params.type
        ? parseInt(this.$route.params.type)
        : "";
      this.$set(this.query, "status", status);
      this.getOrderList();
    },
  },
  created() {
    let status = this.$route.params.type
      ? parseInt(this.$route.params.type)
      : "";
    this.$set(this.query, "status", status);
    this.getOrderList();
  },
  methods: {
    jumpTo(path) {
      this.$router.push({ path });
    },
    async getOrderList() {
      window.scrollTo(0, 0);
      let loading = this.loadingMsg();
      let result = await api.ORDER_LIST(this.query);

      // 更新分页信息
      this.$set(this.query, "current", result.current);
      this.$set(this.query, "length", result.length);
      this.$set(this.query, "pages", result.pages);
      this.$set(this.query, "total", result.total);
      this.$set(this.query, "search", result.search);

      // 更新翻页信息
      this.orderList = result.data;
      loading.close();
    },
  },
};
</script>

<style lang="scss" scoped>
.order-page {
  .order-type {
    display: flex;
    .type {
      display: flex;
      flex: 1;
      height: 36px;
      border: 1px solid #f0f0f0;
      justify-content: center;
      align-items: center;
      font-size: 14px;
      color: #666666;
      cursor: pointer;

      &.active {
        font-weight: 600;
        color: #d52c1a;
        b {
          color: #d52c1a;
          display: inline-block;
        }
      }
      b {
        color: #333;
        margin-left: 16px;
        display: none;
      }
    }
  }
  .order-query {
    display: flex;
    margin: 20px 0;
    align-items: center;
    ::v-deep {
      .el-checkbox {
        margin-left: 21px;
        margin-right: 26px;
      }
      .el-checkbox__label {
        font-size: 16px;
        padding: 0 0 0 16px;
      }
    }
    .select {
      font-size: 16px;
      color: #333333;
    }
    .search {
      width: 320px;
    }
  }
  .no-list {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 100px 0;
    user-select: none;
    img {
      width: 350px;
    }
  }
  .pages-box {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
